<?php
class Ssh {
	private $_aConn = array ();
	private $_rConn = null;
	
	public function connect($sHost, $sUser, $sPassword) {
		try {
			list ( $sHost, $iPort ) = explode ( ':', $sHost );
			$iPort = intval ( $iPort ) ? $iPort : 22;
			$sId = $this->_getId ( $sHost, $iPort );
			if (isset ( $this->_aConn [$sId] )) {
				return $this->_aConn [$sId];
			}
			$rConn = ssh2_connect ( $sHost, $iPort );
			if (! $rConn) {
				throw new ExceptionExt ( 'ssh连接失败' );
			}
			if (! @ssh2_auth_password ( $rConn, $sUser, $sPassword )) {
				throw new ExceptionExt ( 'ssh验证用户名密码错误' );
			} else {
				$this->_aConn [$sId] = $rConn;
				return $rConn;
			}
		} catch ( ExceptionExt $e ) {
			$e->showError ();
		}
	
	}
	
	private function _getId($sHost, $iPort) {
		return md5 ( $sHost . ':' . $iPort );
	}
}